package com.wx.tnsadmin.controller;

import com.wx.common.api.CommonResult;
import com.wx.common.api.ResultCode;
import com.wx.tnsadmin.annotation.Log;
import com.wx.tnsadmin.model.UmsMenuModel;
import com.wx.tnsadmin.service.UmsMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @Author:LLH
 * @Date:2020/8/10
 */

@RequestMapping(value = "/menu")
@RestController
@Api(tags = "后台菜单导航")
public class UmsMenuController {

    @Autowired
    private UmsMenuService umsMenuService;


    @ApiOperation(value = "后台菜单导航", notes = "后台菜单导航")
    @RequestMapping(value = "/findAllMenu", method = RequestMethod.GET)
    public CommonResult findAllMenu () {
        List<UmsMenuModel> list = umsMenuService.findAllMenu ( );
        try {
            if (list == null) {
                return CommonResult.failed ( "菜单导航查询失败!" );
            } else {
                return CommonResult.success ( list,"菜单导航查询成功!" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }

    @PreAuthorize("hasAuthority('tns:menu:addMenu')")
    @Log(operation = "后台新增菜单")
    @ApiOperation(value = "后台新增菜单", notes = "后台新增菜单")
    @RequestMapping(value = "/addMenu", method = RequestMethod.POST)
    public CommonResult addMenu ( @RequestBody(required = false) UmsMenuModel umsMenuModel ) {
        Integer integer = umsMenuService.addMenu ( umsMenuModel );
        try {
            if (integer < 0) {
                return CommonResult.failed ( "新增菜单导航失败!" );
            } else {
                return CommonResult.success ( umsMenuModel,"新增菜单导航成功!" );
            }

        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }

    @Log(operation = "后台修改菜单")
    @ApiOperation(value = "后台修改菜单", notes = "后台修改菜单")
    @RequestMapping(value = "/updateMenu", method = RequestMethod.POST)
    public CommonResult updateMenu ( @RequestBody(required = false) UmsMenuModel umsMenuModel ) {
        Integer integer = umsMenuService.updateMenu ( umsMenuModel );
        try {
            if (integer < 0) {
                return CommonResult.failed ( "修改菜单导航失败!" );
            } else {
                return CommonResult.success ( umsMenuModel,"修改菜单导航成功!" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }


    @PreAuthorize("hasAuthority('tns:menu:deleteMenu')")
    @Log(operation = "后台删除菜单")
    @ApiOperation(value = "后台删除菜单", notes = "后台删除菜单")
    @RequestMapping(value = "/deleteMenu", method = RequestMethod.DELETE)
    public CommonResult deleteMenu ( Integer id ) {
        Integer integer = umsMenuService.deleteMenu ( id );
        try {
            if (integer == 0) {
                return CommonResult.failed ( "删除菜单导航失败!" );
            } else {
                return CommonResult.success ( id,"删除菜单导航成功!" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }


    @PreAuthorize("hasAuthority('tns:menu:deleteMenuList')")
    @Log(operation = "后台批量删除菜单")
    @ApiOperation(value = "后台批量删除菜单", notes = "后台批量删除菜单")
    @RequestMapping(value = "/deleteMenuList", method = RequestMethod.DELETE)
    public CommonResult deleteMenuList ( String list ) {
        try {
            umsMenuService.deleteMenuList ( list );
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
        return CommonResult.success ( "删除菜单导航成功!" );
    }


    @Log(operation = "后台菜单ID查询")
    @ApiOperation(value = "后台菜单ID查询", notes = "后台菜单ID查询")
    @RequestMapping(value = "/findByMenuId", method = RequestMethod.GET)
    public CommonResult findByMenuId ( @RequestParam(required = false) Integer id ) {
        UmsMenuModel byMenuId = umsMenuService.findByMenuId ( id );
        try {
            if (byMenuId == null) {
                return CommonResult.failed ( "菜单导航ID查询失败!" );
            } else {
                return CommonResult.success ( byMenuId,"菜单导航ID查询成功!" );
            }
        } catch (Exception e) {
            e.printStackTrace ( );
            return CommonResult.failed ( ResultCode.FAILED );
        }
    }


}
